//
//  ZSNewsDataView.m
//  fangwangyi
//
//  Created by 臧松 on 16/4/27.
//  Copyright © 2016年 zangsong. All rights reserved.
//

#import "ZSNewsDataView.h"
#import "ZSNewsDataViewCell.h"
#define kZSNewDataViewCellId @"ZSNewDataViewCellId"

@interface ZSNewsDataView ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) NSMutableArray * channels;
@property (nonatomic,strong) NSIndexPath * indexPath;

@end
@implementation ZSNewsDataView
// 懒加载
-(NSMutableArray *)channels{
    if (_channels == nil) {
        _channels = [NSMutableArray array];
    }
    return  _channels;
}
-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
       // self.backgroundColor = [UIColor blueColor];
        // 设置分页
        self.pagingEnabled = YES;
        // 取消弹簧效果
        self.bounces = NO;
        // 取消滚动条
        self.showsHorizontalScrollIndicator = NO;
        // 注册 cell
        [self registerClass:[ZSNewsDataViewCell class] forCellWithReuseIdentifier:kZSNewDataViewCellId];
        // 设置数据源
        self.dataSource = self;
        // 设置代理
        self.delegate = self;
        // 接受新闻频道数据(设置 item 数量)
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setupItemWithNotify:) name:@"channelData" object:nil];
        // 注册接受新闻频道改变的通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changedNewsData:) name:@"channelChanged" object:nil];
        
    }
    return self;
}

// 根据通知传递过来的 indexPath 滚动到相应的 Indexpath
-(void)changedNewsData:(NSNotification*)notify{
    NSIndexPath * indexPath = notify.object;
    // 滚动到相应的新闻主界面
    [self scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}

// 根据通知传过来的数据,设置 item 的数量
-(void)setupItemWithNotify:(NSNotification*)notify{
    // 赋值
    self.channels = notify.object;
    //NSLog(@"%lu",(unsigned long)self.channels.count);
    // 刷新数据
    [self reloadData];
}
#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.channels.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZSNewsDataViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:kZSNewDataViewCellId forIndexPath:indexPath];
   // NSLog(@"%@",indexPath);
    self.indexPath = indexPath;
   // NSLog(@"%@",self.indexPath);
    // 将新闻频道数据传递出去
    ZSChannel * channel = self.channels[indexPath.item];
    cell.channel = channel;
    
    return cell;
    
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    NSLog(@"%@",self.indexPath);
    [[NSNotificationCenter defaultCenter]postNotificationName:@"newsDataChanged" object:self.indexPath];
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    NSLog(@"%@",self.indexPath);
    [[NSNotificationCenter defaultCenter]postNotificationName:@"newsDataChanged" object:self.indexPath];

}
@end
